<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 文章管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{$find.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-inline" style="width: 300px;">
            <input type="text" name="title" value="{$find.title}" lay-verify="required" placeholder="标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">简短描述</label>
        <div class="layui-input-inline" style="width: 300px;">
            <textarea name="describe" lay-verify="required" placeholder="备注" autocomplete="off" class="layui-textarea">{$find.describe}</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-inline">
            <input type="text"  name="img" value="{$find.img}" lay-verify="required" placeholder="请上传图片" autocomplete="off" class="layui-input">
        </div>
        <button style="float: left;" type="button" class="layui-btn" id="demo">上传图片</button>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接</label>
        <div class="layui-input-inline">
            <input type="text" name="link" value="{$find.link}" layadmin-event="page"  placeholder="链接" autocomplete="off" class="layui-input" >
            <input type="hidden" name="link_column" value="{$find.link_column}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">发送对象</label>
        <div class="layui-input-block">
            <input type="radio" name="obj" value="1" lay-filter="obj" title="全部" {if condition="!$find"}checked{/if}{eq name="find.obj" value="1"}checked{/eq}>
            <input type="radio" name="obj" value="2" lay-filter="obj" title="指定用户" {eq name="find.obj" value="2"}checked{/eq}>
        </div>
    </div>
    <div id="obj" class="layui-hide">
        <div class="layui-form-item">
            <label class="layui-form-label">指定用户</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" ts-selected="" id="user" lay-verify="required" placeholder="请选择用户" autocomplete="off">
                <input type="hidden" name="user" value="">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'upload', 'tableSelect'], function(){
        var $ = layui.$,
                form = layui.form,
                tableSelect = layui.tableSelect,
                upload = layui.upload;

        tableSelect.render({
            elem: '#user',	//定义输入框input对象
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'info',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '搜索用户名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: "{:url('User/index')}",
                method: 'post',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'name', title: '用户名称', width: 400},
                    {field: 'time', title: '加入时间', width: 200, templet: "#time"}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var field = data.data;
                var pid = [];
                var name = [];
                for (var i = 0; i < field.length; i++){
                    pid.push(field[i].id);
                    name.push(field[i].name);
                }
                $("input[name=user]").val(pid.join(','));
                $("#user").val(name.join(','));
            }
        });

        //普通图片上传
        upload.render({
            elem: '#demo'
            ,url: '/admin/Common/upload/filename/message'
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg(res.msg);
                }
                //上传成功
                $("input[name=img]").val(res.img);
                layer.msg('上传成功');
            }
        });

        if ('{$find.obj}' == 1){
            $("#obj").addClass('layui-hide');
            $("#user").removeAttr("lay-verify");
        } else if ('{$find.obj}' == 2){
            $("#obj").removeClass("layui-hide");
            $("#user").attr("lay-verify", "required");
        }

        form.on('radio(obj)', function(data){
            if (data.value == 1){
                $("#obj").addClass('layui-hide');
                $("#user").removeAttr("lay-verify");
            }else{
                $("#obj").removeClass("layui-hide");
                $("#user").attr("lay-verify", "required");

            }
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('System/message_add')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('添加成功');
                        setTimeout(function () {
                            open_window.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('添加失败');
                    }
                }
            });
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('System/message_edit')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('修改成功');
                        setTimeout(function () {
                            open_window.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('修改失败');
                    }
                }
            });
        });
    })

    var open_window;
    function child(d) {
        open_window = d;
    }
</script>
</body>
</html>